﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.DataModel;
using System.Xml;
using System.IO;
using SharedLib.Helpers;

namespace Tracy.DataAccess
{
    public class FilterProvider
    {
        //private string DataFolder {
        //    get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Data"); } 
        //}
        private string FilterSetFile
        {
            get { return AppDataHelper.GetAppDataFile("Data\\FilterSets.xml"); }
        }

        XmlDocument doc;
        XmlElement root;

        

        public void LoadItems()
        {
            //if (!Directory.Exists(DataFolder))
            //{
            //    Directory.CreateDirectory(DataFolder);
            //}

            doc = new XmlDocument();
            if (!File.Exists(FilterSetFile))
            {
                root = doc.AppendChild(doc.CreateElement("entries")) as XmlElement;
                doc.Save(FilterSetFile);
            }
            else
            {
                doc.Load(FilterSetFile);
                root = doc.SelectSingleNode("entries") as XmlElement;
                if (root == null) throw new Exception("Root Node \"entries\" not found!");
            }
        }

        public void SaveItems()
        {
            doc.Save(FilterSetFile);
        }

        public List<FilterSet> GetFilterSets(Entry entry)
        {
            List<FilterSet> rtn = new List<FilterSet>();
            foreach (XmlNode node in root.SelectNodes(String.Format("entry[@id='{0}']/filter-sets/filter-set", entry.ID)))
            {              
                rtn.Add(new FilterSet(node));
            }
            return rtn;
        }

        public void SetFilterSets(List<FilterSet> filterSets, Entry entry)
        {
            XmlElement entryNode = root.SelectSingleNode(String.Format("entry[@id='{0}']", entry.ID)) as XmlElement ;
            if (entryNode != null)
            {
                root.RemoveChild(entryNode);
            }
            
            entryNode = root.AppendChild(doc.CreateElement("entry")) as XmlElement;
            entryNode.SetAttribute("id", entry.ID);
            XmlElement filterSetsNode = entryNode.AppendChild(doc.CreateElement("filter-sets")) as XmlElement;

            if (filterSets != null)
            {
                foreach (FilterSet filterSet in filterSets)
                {
                    filterSetsNode.AppendChild(filterSet.ToXmlNode(doc));
                }
            }
            SaveItems();
        }

        public static bool IsMatch(string title, IHasAliases filterItem)
        {
            if (String.Equals(filterItem.Name, "*")) 
                return true;

            if (title.Contains(filterItem.Name.ToUpper()))
            {
                return true;
            }

            foreach (string alias in filterItem.Aliases)
            {
                if (title.Contains(alias.ToUpper()))
                {
                    return true;
                }
            }

            return false;
        }

        public static List<FanSubGroup> GetFanSubGroups(string title)
        {
            List<FanSubGroup> rtn = new List<FanSubGroup>();
            List<FanSubGroup> fanSubGroups = TracyFacade.Instance.FilterManager.FanSubGroupProvider.GetItems;
            foreach (FanSubGroup fanSubGroup in fanSubGroups)
            {
                if (IsMatch(title, fanSubGroup))
                {
                    rtn.Add(fanSubGroup);
                }
            }

            return rtn;
        }

        public static Format GetFormat(string title)
        {
            List<Format> formats = TracyFacade.Instance.FilterManager.FormatProvider.GetItems;
            foreach (Format format in formats)
            {
                if (IsMatch(title, format))
                {
                    return format;
                }
            }
            Format unknown = new Format();
            unknown.Name = "*";
            return unknown;
        }

        public static Resolution GetResolution(string title)
        {
            List<Resolution> resolutions = TracyFacade.Instance.FilterManager.ResolutionProvider.GetItems;
            foreach (Resolution resolution in resolutions)
            {
                if (IsMatch(title, resolution))
                {
                    return resolution;
                }
            }
            Resolution unknown = new Resolution();
            unknown.Name = "*";
            return unknown;
        }
    }


}
